<?php

namespace App\Models;

use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/*数字资源-资源阅读表*/

class ResourceBookRead extends BaseModel
{
    use HasFactory;

    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'change_time';


    protected $table = 'resource_book_read';


    /**
     * 添加所有的阅读记录（阅读时长）
     * @param resource_guid 资源guid
     * @param resource_business_id 资源商id
     * @param big_type_id 资源大类型id
     * @param user_id 用户id
     */
    public function addAllReadDuration($resource_guid, $resource_business_id, $big_type_id = null, $user_id = null, $read_duration = null, $date = null)
    {
        $date = $date ? $date : date('Y-m-d');
        $obj = new self();
        $obj->addReadBook($resource_guid, $resource_business_id, $big_type_id, $read_duration);

        $resourceBookReadDayModel = new ResourceBookReadDay();
        $resourceBookReadDayModel->addReadBookDay($resource_guid, $resource_business_id, $big_type_id,  $date, $read_duration);

        if ($user_id) {
            $resourceUserReadModel = new ResourceUserRead();
            $resourceUserReadModel->addUserReadBook($user_id, $read_duration);

            $resourceUserReadBookModel = new ResourceUserReadBook();
            $resourceUserReadBookModel->addUserReadBook($resource_guid, $resource_business_id, $big_type_id, $user_id, $read_duration);

            $resourceUserReadBookDayModel = new ResourceUserReadBookDay();
            $resourceUserReadBookDayModel->addUserReadBookDay($resource_guid, $resource_business_id, $big_type_id, $user_id, $date, $read_duration);
        }
    }



    /**
     * 添加用户阅读时间
     * @param read_duration 阅读时长  单位分钟
     */
    public function addReadBook($resource_guid, $resource_business_id, $big_type_id = null, int $read_duration = 1)
    {
        $res = $this->where('resource_guid', $resource_guid)->where('resource_business_id', $resource_business_id)->first();
        if (empty($res)) {
            $res = $this;
        } else {
            $read_duration = $res->read_duration + $read_duration;
        }
        $res->resource_guid = $resource_guid;
        $res->resource_business_id = $resource_business_id;
        $res->big_type_id = $big_type_id;
        $res->read_duration = $read_duration; //阅读时长
        $res->save();
        return true;
    }



    /**
     * 获取阅读历史排行榜
     * @param start_time 开始时间
     * @param end_time 结束时间
     * @param type 以不同时间形式展示数据 year month week day  类型和时间一起上传，默认以类型为准
     * @param resource_business_id 资源商
     * @param big_type_id 系统大类
     * @param limit
     */
    public function getReadBookRank($start_time = null, $end_time = null, $type = 'year', $resource_business_id = null, $big_type_id = null, $limit = 10)
    {
        if ($type == 'year') {
            $start_time = strtotime(date('Y-01-01 00:00:00'));
            $end_time = strtotime(date('Y-m-d 23:59:59'));
        } elseif ($type == 'month') {
            $start_time = strtotime(date('Y-m-01 00:00:00'));
            $end_time = strtotime(date('Y-m-d 23:59:59'));
        } elseif ($type == 'week') {
            $start_time = strtotime(date('Y-m-d 00:00:00', strtotime('-7 day')));
            $end_time = strtotime(date('Y-m-d 23:59:59'));
        } else {
            $start_time = strtotime(date('Y-m-d 00:00:00'));
            $end_time = strtotime(date('Y-m-d 23:59:59'));
        }
        $res = $this->from('resource_book_read as b')
            ->select('r.id', 'r.guid', 'r.title', 'r.author', 'r.img', 'b.change_time', 'b.read_duration')
            ->join('resource_total as r', 'r.guid', '=', 'b.resource_guid')
            ->join('resource_business_name as n', 'n.id', '=', 'r.resource_business_id')
            ->where(function ($query) use ($resource_business_id, $big_type_id) {
                if ($resource_business_id) {
                    $query->where('r.resource_business_id', $resource_business_id);
                }
                if ($big_type_id) {
                    $query->where('r.big_type_id', $big_type_id);
                }
            })
            ->where(function ($query) use ($start_time, $end_time) {
                if ($start_time && $end_time) {
                    $query->whereBetween('b.change_time', [$start_time, $end_time]);
                }
            })
            ->where('n.is_del' ,1)
            ->where('r.is_del' ,1)
            ->orderByDesc('read_duration')
            ->orderByDesc('change_time')
            ->paginate($limit)
            ->toArray();
        return $res;
    }
}
